//
//  MySelfViewController.m
//  MIDI
//
//  Created by 千行时线 on 15/9/21.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "MySelfViewController.h"
#import "AppConfig.h"
#import "MySelfCell.h"
#import "MyOrderViewController.h"
#import "MyMessageViewController.h"
#import "AboutMIDIViewController.h"
#import "ShowApplyViewController.h"
#import "SystemSetViewController.h"
#import "MyTestViewController.h"
#import "SelfInformationViewCtrler.h"
#import "UserDataSingleton.h"
#import "LoginViewController.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "UserDataSingleton.h"
#import "SelfInfoModel.h"
#import "UIImageView+WebCache.h"
#import "CommFunc.h"

@interface MySelfViewController ()<UITableViewDataSource , UITableViewDelegate , QXNetWorkToolDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong , nonatomic) QXNetWorkTool *netTool;
@end

@implementation MySelfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.netTool = [[QXNetWorkTool alloc]init];
    self.netTool.QXNetWorkDelegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
    [self.myTableView reloadData];
    [self reqSelfInfo];
}

-(void) configNavigationItem
{
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.tabBarController.navigationItem.title = @"我的";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    
    UIButton *barButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 15)];
    
    [barButton setImage:[UIImage imageNamed:@"btn_navigation_back"] forState:UIControlStateNormal];
    [barButton setImage:[UIImage imageNamed:@"btn_navigation_back"] forState:UIControlStateNormal];
    [barButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
    [barButton addTarget:self action:@selector(pressLeft:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setLeftBarButtonItem:barItem];
    
    
}

-(void)pressLeft:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([UIScreen mainScreen].bounds.size.width == 320)
    {
        return 587;
    }
    else
        return [UIScreen mainScreen].bounds.size.height - 64;
    return 587;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MySelfCell * cell = [MySelfCell loadFromeXib];
    [self configCell:cell];
    return cell;
}

#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void)clickBtn : (UIButton*)sender
{
    switch (sender.tag) {
        case 1:
        {
            UserDataSingleton *userData = [UserDataSingleton shareInstance];
            if(![userData isLogin])
            {
                [userData pleaseLogin:self];
                return;
            }
            
            MyTestViewController *obj = [MyTestViewController create];
            obj.isReturnTOShouYe = NO;
            [self.navigationController pushViewController:obj animated:YES];
        }
            break;
        case 2:
        {
            UserDataSingleton *userData = [UserDataSingleton shareInstance];
            if(![userData isLogin])
            {
                [userData pleaseLogin:self];
                return;
            }
            
            [self.navigationController pushViewController:
             [MyOrderViewController create] animated:YES];
        }
            break;
        case 3:
        {
            UserDataSingleton *userData = [UserDataSingleton shareInstance];
            if(![userData isLogin])
            {
                [userData pleaseLogin:self];
                return;
            }
            
            [self.navigationController pushViewController:
             [MyMessageViewController create] animated:YES];
        }
            break;
        case 4:
        {
            [self.navigationController pushViewController:
             [AboutMIDIViewController create] animated:YES];
        }
            break;
        case 5:
        {
//            UserDataSingleton *userData = [UserDataSingleton shareInstance];
//            if(![userData isLogin])
//            {
//                [userData pleaseLogin:self];
//                return;
//            }
            
            [self.navigationController pushViewController:
             [ShowApplyViewController create] animated:YES];
        }
            break;
        case 6:
        {
            [self.navigationController pushViewController:
             [SystemSetViewController create] animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)tabIconImage : (UITapGestureRecognizer *) sender
{
    UserDataSingleton * userdata = [UserDataSingleton shareInstance];
    if(![userdata isLogin])
    {
        [userdata pleaseLogin:self];
        return;
    }
    [self.navigationController pushViewController:[SelfInformationViewCtrler create] animated:YES];
}


-(void)configCell : (MySelfCell*)cell
{
    [cell.myTestBtn setTag:1];
    [cell.myOrderBtn setTag:2];
    [cell.myMessageBtn setTag:3];
    [cell.aboutMIDIBtn setTag:4];
    [cell.applyShowBtn setTag:5];
    [cell.SysTemSettingBtn setTag:6];
    [cell.myTestBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell.myOrderBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell.myMessageBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell.aboutMIDIBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell.applyShowBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell.SysTemSettingBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tabIconImage:)];
    [cell.myIco addGestureRecognizer:tapGestureRecognizer];
    cell.myIco.userInteractionEnabled = YES;
    [cell.myIco.layer setCornerRadius:cell.myIco.frame.size.width/2];
    cell.myIco.contentMode = UIViewContentModeScaleAspectFill;
    cell.myIco.clipsToBounds = YES;
    UserDataSingleton * userdata = [UserDataSingleton shareInstance];
    if([userdata isLogin])
    {
        cell.titleIco.hidden = NO;
        cell.titleLb.hidden = NO;
        if(userdata.isGotSelfInfo)
        {
            [cell.midiIdLb setText:[@"Midi ID: " stringByAppendingString:userdata.midi_id]];
            NSString *icoUrl = userdata.head_img;
            if(icoUrl == nil || [icoUrl isEqual:@""])
            {
                [cell.myIco setImage:[UIImage imageNamed:@"ico_selfinfo_nonoe"]];
            }
            else
            {
                [cell.myIco sd_setImageWithURL:[NSURL URLWithString:userdata.head_img]];
            }
        }
    }
    else
    {
        cell.titleIco.hidden = YES;
        cell.titleLb.hidden = YES;
        [cell.midiIdLb setText:@"登陆/注册"];
        [cell.myIco setImage:[UIImage imageNamed:@"ico_selfinfo_nonoe"]];
    }
}

#pragma mark --- QxnetTool-------------------
-(void)GetFailResponse:(id)Response andTag:(int)tag
{
    
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    
    if(tag == 1)
    {
        SelfInfoModel * data = nil;
        for(NSDictionary * dic in Response)
        {
            data = [SelfInfoModel MyModelWithDict:dic];
            break;
        }
        if(data == nil)
            return;
        UserDataSingleton *userdata = [UserDataSingleton shareInstance];
        userdata.isGotSelfInfo = YES;
        userdata.midi_id = data.midi_id;
        userdata.sex = data.sex;
        userdata.eamil = data.eamil;
        userdata.people = data.people;
        userdata.nationality = data.nationality;
        userdata.id_card = data.id_card;
        userdata.head_img = data.head_img;
        userdata.nickname = data.nickname;
        userdata.real_name = data.real_name;
        userdata.phone = data.phone;
        userdata.birthday = data.birthday;
        [self.myTableView reloadData];
    }
    
}
//请求个人数据
-(void)reqSelfInfo
{
    UserDataSingleton * userdata = [UserDataSingleton shareInstance];
    if(![userdata isLogin])
        return;
    if(userdata.isGotSelfInfo && !userdata.isSelfInfoChange)
        return;
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:GetSelfInfoInterface andParameter:@[@{@"user_id":userdata.user_id}] tag:1];
    
}

@end















